package com.jiulan.hearthearing.ui.my.view;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseBottomDialog;
import com.jiulan.hearthearing.ui.my.activity.PersonInfoActivity;
import com.jiulan.hearthearing.ui.my.adapter.PersonInfoAdapter;

import java.util.List;

public class PersonInfoSetDialog extends BaseBottomDialog {
    private Context context;
    private int action;
    private String choose = "";
    private List<String> data;
    private PersonInfoAdapter infoAdapter;

    private RecyclerView mRecyclerView;
    private TextView tvCancel;

    public PersonInfoSetDialog(Context context, int action, List<String> data, String choose) {
        this.context = context;
        this.action = action;
        this.data = data;
        this.choose = choose;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_person_info;
    }

    @Override
    public void bindView(View v) {
        mRecyclerView = v.findViewById(R.id.mRecyclerView);
        tvCancel = v.findViewById(R.id.tvCancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        infoAdapter = new PersonInfoAdapter(data);
        infoAdapter.setClickPosition(choose);
        mRecyclerView.setAdapter(infoAdapter);
        infoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                infoAdapter.setClickPosition(data.get(position));
                infoAdapter.notifyItemChanged(position);
                sendBroadcast(data.get(position));
                dismiss();
            }
        });
    }

    private void sendBroadcast(String value) {
        Intent intent = new Intent(PersonInfoActivity.LOCAL_BROADCAST);
        intent.putExtra("action", action);
        intent.putExtra("value", value);
        PersonInfoActivity.localBroadcastManager.sendBroadcast(intent);
    }
}
